<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" header-align="center" align="center"></el-table-column>
      <el-table-column prop="title" label="标题" show-overflow-tooltip header-align="center"
                       align="center"></el-table-column>
      <el-table-column prop="price" label="价格" show-overflow-tooltip header-align="center"></el-table-column>
      <el-table-column prop="oldPrice" label="原价" header-align="center" align="center"></el-table-column>
      <el-table-column prop="saleCount" label="销量" header-align="center" align="center"></el-table-column>
      <el-table-column prop="num" label="库存" header-align="center" align="center"></el-table-column>
      <el-table-column prop="proCategoryId" label="类编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="urls" label="图片地址" header-align="center" align="center"></el-table-column>
      <el-table-column label="操作" width="200" header-align="center" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle @click="showEditFormDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 以下是修改商品的弹出表单Dialog -->
    <el-dialog title="修改商品" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="editForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input v-model="editForm.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="原价" :label-width="formLabelWidth">
          <el-input v-model="editForm.oldPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="销量" :label-width="formLabelWidth">
          <el-input v-model="editForm.saleCount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类编号" :label-width="formLabelWidth">
          <el-input v-model="editForm.proCategoryId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片地址" :label-width="formLabelWidth">
          <el-input v-model="editForm.urls" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id: '',
        title: '',
        price: '',
        oldPrice: '',
        saleCount: '',
        num: '',
        proCategoryId: '',
        urls: '',
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    showEditFormDialog(product) {
      let url = 'http://localhost:9999/products/' + product.id;
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadProductList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9999/products/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);

      this.axios.post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改商品成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadProductList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadProductList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
    openDeleteConfirm(product) {
      let message = '此操作将永久删除[' + product.name + ']商品，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(product);
      }).catch(() => {
      });
    },
    handleDelete(product) {
      let url = 'http://localhost:9999/products/' + product.id + '/delete';
      console.log('url = ' + url);
      this.axios.post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除[' + product.name + ']商品成功！',
            type: 'success'
          });
          this.loadProductList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadProductList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    loadProductList() {
      let url = 'http://localhost:9999/products';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
        }
      });
    }
  },
  mounted() {//挂载
    this.loadProductList();
  }
}
</script>